package com.rr.openApi.user.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.rr.frame.common.FinalDes;
import com.rr.frame.exception.ServiceException;
import com.rr.frame.utils.InitRequestParameter;
import com.rr.openApi.user.service.UserService;



@RestController
@RequestMapping("/openApi/user")
public class UserController {
	private Logger logger = Logger.getLogger(UserController.class);
	
	@Autowired
    private UserService userService;
	
	 @RequestMapping(value="/login2",method=RequestMethod.POST)
	 @ResponseBody
	 public void getUserInfo(@RequestBody String body) throws UnsupportedEncodingException {
		 logger.info(body);
		 body = URLDecoder.decode(body,"utf-8");
		 logger.info("body:"+ body);
	    }
	 
	 
	 @RequestMapping(value="/login",method=RequestMethod.POST)
	 public Map<String,Object> login(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("登录参数输入=====>" + paraMap);
	            if(null==paraMap.get("username")||"".equals(paraMap.get("username"))){
	 				throw new ServiceException("FA_FAILED_USERNAME_NOT_NULL", "username不能为空");
	 			}
	            if(null==paraMap.get("password")||"".equals(paraMap.get("password"))){
		 			throw new ServiceException("FA_FAILED_PASSWORD_NOT_NULL", "password不能为空");
		 		}
	           Map<String, Object> data = userService.login(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("登录参数输出=====>" + resultMap);
	     return resultMap;
	}
	 
	 @RequestMapping(value="/isUserNameExist",method=RequestMethod.POST)
	 public Map<String,Object> isUserNameExist(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("isUserNameExist参数输入=====>" + paraMap);
	            if(null==paraMap.get("username")||"".equals(paraMap.get("username"))){
	 				throw new ServiceException("FA_FAILED_USERNAME_NOT_NULL", "username不能为空");
	 			}
	           Boolean data = userService.isUserNameExist(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("isUserNameExist参数输出=====>" + resultMap);
	     return resultMap;
	}
	 
	 @RequestMapping(value="/signIn",method=RequestMethod.POST)
	 public Map<String,Object> signIn(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("signIn参数输入=====>" + paraMap);
	            if(null==paraMap.get("username")||"".equals(paraMap.get("username"))){
	 				throw new ServiceException("FA_FAILED_USERNAME_NOT_NULL", "username不能为空");
	 			}
	            if(null==paraMap.get("password")||"".equals(paraMap.get("password"))){
	 				throw new ServiceException("FA_FAILED_PASSWORD_NOT_NULL", "密码不能为空");
	 			}
	            if(null==paraMap.get("realName")||"".equals(paraMap.get("realName"))){
	 				throw new ServiceException("FA_FAILED_REALNAME_NOT_NULL", "昵称不能为空");
	 			}
	           String data = userService.addUser(paraMap);
	           resultMap.put("data", data);
	           resultMap.put(FinalDes.STATUS_CODE, FinalDes.OK_CODE);
	           resultMap.put(FinalDes.STATUS_MSG, "");
	             
	     } catch (ServiceException e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, e.getStatus());
			resultMap.put(FinalDes.STATUS_MSG, e.getMessage());
			logger.info("ssss");
		 } catch (Exception e) {
			e.printStackTrace();
			resultMap.put(FinalDes.STATUS_CODE, FinalDes.SYSTEM_ERROR_STATUS);
			resultMap.put(FinalDes.STATUS_MSG, FinalDes.SYSTEM_ERROR_INFO);
	     }
	     logger.info("signIn参数输出=====>" + resultMap);
	     return resultMap;
	}
	 
	 @RequestMapping(value="/login3",method=RequestMethod.POST)
	 public void processRequest22(HttpServletRequest request) {
		 Map<String, Object> resultMap = new HashMap<String, Object>();
		 Map<String, Object> paraMap = new HashMap<String, Object>();
		// Map<String, Object> parameter = new HashMap<String, Object>();
	     try {
	         	request.setCharacterEncoding("UTF-8");
	            paraMap = InitRequestParameter.initParameters(request);  
	            logger.info("登录参数输入=====>" + paraMap);
	            
	             
	     } catch (Exception e) {
	     }
	}

}